/*
* ----------------------------------------------------------------------------
* THE COFFEEWARE LICENSE (Revision 1):
* <ihsan@kehribar.me> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a coffee in return.
* -----------------------------------------------------------------------------
* This library is based on this library: 
*   https://github.com/aaronds/arduino-nrf24l01
* Which is based on this library: 
*   http://www.tinkerer.eu/AVRLib/nRF24L01
* -----------------------------------------------------------------------------
*/
#ifndef NRF24
#define NRF24

#include "nRF24L01.h"
#include <stdint.h>
#include <stdbool.h>

#define LOW 0
#define HIGH 1

#define NRF24_ADDR_LEN 5
#define NRF24_CONFIG_ALL_INTS ((1<<EN_CRC) | (0<<CRCO))
#define NRF24_CONFIG_NO_INTS (NRF24_CONFIG_ALL_INTS | (1<<MASK_MAX_RT) | (1<<MAS_TX_DS) | (1<<MASK_RX_DR))

#define NRF24_TRANSMISSON_OK 0
#define NRF24_MESSAGE_LOST   1

#define BAD_PAYLOAD_LENGTH	0xFF

/* adjustment functions */
void    nrf24_init(void);
void    nrf24_rx_address(uint8_t* adr, uint8_t pipe);
void    nrf24_tx_address(uint8_t* adr);
void    nrf24_config(uint8_t channel);

/* state check functions */
uint8_t nrf24_dataReady(void);
uint8_t nrf24_isSending(void);
uint8_t nrf24_getStatus(void);
uint8_t nrf24_rxFifoEmpty(void);
bool 	nrf24_rf_signal_present(void);

/* core TX / RX functions */
void    nrf24_send(uint8_t* value, uint8_t length);
void    nrf24_getData(uint8_t* data, uint8_t length);

/* use in dynamic length mode */
uint8_t nrf24_payloadLength(void);

/* what pipe the data in the rx fifo was from */
uint8_t nrf24_getRxPipe(void);

/* post transmission analysis */
uint8_t nrf24_lastMessageStatus(void);
uint8_t nrf24_retransmissionCount(void);

/* power management */
void    nrf24_powerUpRx(void);
void    nrf24_powerUpTx(void);
void    nrf24_powerDown(void);

/* low level interface ... */
uint8_t spi_transfer(uint8_t tx);
void    nrf24_transmitSync(uint8_t* dataout, uint8_t len);
void    nrf24_transferSync(uint8_t* dataout, uint8_t* datain, uint8_t len);
void    nrf24_configRegister(uint8_t reg, uint8_t value);
void    nrf24_readRegister(uint8_t reg, uint8_t* value, uint8_t len);
void    nrf24_writeRegister(uint8_t reg, uint8_t* value, uint8_t len);
void 	nrf24_setBits_reg(uint8_t reg, uint8_t bits);
void 	nrf24_clrBits_reg(uint8_t reg, uint8_t bits);

/* -------------------------------------------------------------------------- */
/* You should implement the platform spesific functions in your code */
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/* In this function you should do the following things:
 *    - Set MISO pin input
 *    - Set MOSI pin output
 *    - Set SCK pin output
 *    - Set CSN pin output
 *    - Set CE pin output     */
/* -------------------------------------------------------------------------- */
void nrf24_setupPins(void);

/* -------------------------------------------------------------------------- */
/* nrf24 CE pin control function
 *    - state:1 => Pin HIGH
 *    - state:0 => Pin LOW     */
/* -------------------------------------------------------------------------- */
//void nrf24_ce_digitalWrite(uint8_t state);

/* -------------------------------------------------------------------------- */
/* nrf24 CE pin control function
 *    - state:1 => Pin HIGH
 *    - state:0 => Pin LOW     */
/* -------------------------------------------------------------------------- */
//void nrf24_csn_digitalWrite(uint8_t state);

/* -------------------------------------------------------------------------- */
/* nrf24 SCK pin control function
 *    - state:1 => Pin HIGH
 *    - state:0 => Pin LOW     */
/* -------------------------------------------------------------------------- */
//void nrf24_sck_digitalWrite(uint8_t state);

/* -------------------------------------------------------------------------- */
/* nrf24 MOSI pin control function
 *    - state:1 => Pin HIGH
 *    - state:0 => Pin LOW     */
/* -------------------------------------------------------------------------- */
//void nrf24_mosi_digitalWrite(uint8_t state);

/* -------------------------------------------------------------------------- */
/* nrf24 MISO pin read function
 *		- returns: Non-zero if the pin is high */
/* -------------------------------------------------------------------------- */
//uint8_t nrf24_miso_digitalRead(void);

/* Pin Manipulation Macros */
#define nrf24_csn_make(state)	do{	\
	if(state)								\
		PORTB |= _BV(PORTB2);				\
	else									\
		PORTB &= ~_BV(PORTB2);				\
}while(0)

#define nrf24_ce_make(state)	do{	\
	if(state)								\
		PORTB |= _BV(PORTB1);				\
	else									\
		PORTB &= ~_BV(PORTB1);				\
}while(0)

#endif
